<template>
  <a-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model :model="model" :form="form" :rules="rules" ref="form" layout="vertical">
        <a-row class="form-row" :gutter="16">
          <a-col :span="6">
            <a-form-model-item label="公司" prop="workshop">
              <j-dict-select-tag
                dictCode="crm_company,name,id"
                v-model="model.company"
                placeholder="请选择组织"
              >
              </j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="车间" prop="workshop">
              <j-dict-select-tag placeholder="请选择车间" v-model="model.workshop" dictCode="workshop" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="工序" prop="workshop">
              <j-dict-select-tag
                dictCode="working_procedure"
                v-model="model.workingProcedure"
                placeholder="请选择工序"
                disabled
              >
              </j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="开始时间" prop="startTime">
              <a-date-picker placeholder="请输入" show-time v-model="startTime" style="width: 100%"
                             @change="changeStartTime"/>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="16">
          <a-col :span="6">
            <a-form-model-item label="结束时间" prop="endTime">
              <a-date-picker placeholder="请输入" show-time v-model="endTime" style="width: 100%" @change="changeEndTime"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="车号" prop="deviceNo">
              <a-input placeholder="请输入" v-model="model.deviceNo" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="机型" prop="deviceModel">
              <a-input placeholder="请输入" v-model="model.deviceModel" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="品种类型" prop="productCategory">
              <a-input placeholder="请输入" v-model="model.productCategory" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="16">
          <a-col :span="6">
            <a-form-item label="品种">
              <j-dict-select-tag placeholder="请选择品种" v-model="model.productType" dictCode="crm_product_type_cotton,product_type,id"/>
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-form-item label="定长">
              <a-input
                v-model="model.length"
                placeholder="请输入"
              />
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-form-item label="定量">
              <a-input
                v-model="model.ration"
                placeholder="请输入"
              />
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-form-item label="定量范围">
              <a-input
                v-model="model.rationRange"
                placeholder="请输入"
              />
            </a-form-item>
          </a-col>

        </a-row>
        <a-row :gutter="16">
          <a-col :span="6">
            <a-form-item label="线速度（m/min）">
              <a-input
                v-model="model.linearVelocity"
                placeholder="请输入"
              />
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-form-item label="线速度范围">
              <a-input
                v-model="model.linearVelocityRange"
                placeholder="请输入"
              />
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-form-item label="输出棉条桶规格">
              <a-input
                v-model="model.cottonBarrelSize"
                placeholder="请输入"
              />
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-form-item label="定额（12小时）">
              <a-input
                v-model="model.weight"
                placeholder="请输入"
              />
            </a-form-item>
          </a-col>

        </a-row>
        <a-row :gutter="16">
          <a-col :span="6">
            <a-form-item label="定台">
              <a-input
                v-model="model.fixedMachinesNum"
                placeholder="请输入"
              />
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-form-item label="单价系数">
              <a-input
                v-model="model.unitPriceCoefficient"
                placeholder="请输入"
              />
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-form-item label="品种系数">
              <a-input
                v-model="model.productTypeCoefficient"
                placeholder="请输入"
              />
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-form-item label="产量单价（元/千米）">
              <a-input
                v-model="model.productionUnitPrice"
                placeholder="请输入"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="16">
          <a-col :span="6">
            <a-form-item label="清洁单价（元/台）">
              <a-input
                v-model="model.cleanUnitPrice"
                placeholder="请输入"
              />
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-form-item label="条桶规格">
              <a-input
                v-model="model.barrelSize"
                placeholder="请输入"
              />
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-form-item label="机台系数">
              <a-input
                v-model="model.machineCoefficient"
                placeholder="请输入"
              />
            </a-form-item>
          </a-col>

          <a-col :span="6">
            <a-form-item label="滤层台数">
              <a-input
                v-model="model.filterLayerNum"
                placeholder="请输入"
              />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import JEditor from '@/components/jeecg/JEditor'
import JSwitch from '@/components/jeecg/JSwitch'

import moment from 'moment'
import { getShiftSystem, productTypeSpoolingList, productTypeSpunYarn } from '../../../../api/customApi'

export default {
  name: 'DeviceModal',
  components: {
    JEditor,
    JSwitch,
  },
  props: {
    planTime: String,
  },
  data() {
    return {
      title: '操作',
      visible: false,
      time: null,
      workHours: null,
      machineHours: null,
      disable: true,
      startTime:'',
      endTime:'',
      model: {
        // workshop: '',
        // productType: '',
        // workingProcedure: '2',
        // productCategory: '',
        // length: '',
        // ration: '',
        // rationRange: '',
        // linearVelocity: '',
        // linearVelocityRange: '',
        // cottonBarrelSize: '',
        // weight: '',
        // fixedMachinesNum: '',
        // unitPriceCoefficient: '',
        // productTypeCoefficient: '',
        // lookMachineUnitPrice: '',
        // rowBagUnitPrice: '',
        // filterLayerUnitPrice: '',
        // machineCoefficient: '',
        // filterLayerNum: '',
      },
      confirmLoading: false,
      form: this.$form.createForm(this),
      url: {
        add: '/crm_product_type_params_cotton/crmProductTypeParamsCotton/add',
        edit: '/crm_product_type_params_cotton/crmProductTypeParamsCotton/edit',
      },
    }
  },
  created() {
  },
  methods: {
    changeStartTime(date, dateString){
      this.model.startTime = dateString
    },
    changeEndTime(date, dateString){
      this.model.endTime = dateString
    },
    changeWorkHours(date, dateString) {
      this.model.workHours = dateString + ' 00:00:00'
      // this.model.workHours = new Date(dateString).getTime()
    },
    changeMachineHours(date, dateString) {
      this.model.machineHours = dateString + ' 00:00:00'
      // this.model.machineHours = new Date(dateString).getTime()
    },
    changeProductType(val) {
      productTypeSpoolingList({ id: val }).then((res) => {
        // this.model = res.result.records[0]
        this.model.productCategory = res.result.records[0].productCategory
        this.model.productBranch = res.result.records[0].productBranch
        this.model.productTypeCoefficient = res.result.records[0].productTypeCoefficient
        this.model.unitPriceCoefficient = res.result.records[0].unitPriceCoefficient
        this.model.ingots = res.result.records[0].ingots
        this.model.fixedMachinesNum = res.result.records[0].fixedMachinesNum
        this.model.outputPrice = res.result.records[0].outputPrice
        this.model.changeTubeBoxPrice = res.result.records[0].changeTubeBoxPrice
        this.model.originalPrice = res.result.records[0].originalPrice
      })
    },
    changeTime(date, dateString) {
      this.model.planTime = dateString
    },
    add() {
      this.disable = false
      this.model.workingProcedure = 2
      this.model.company = '1771136798305476610'
      this.startTime = this.planTime + " 00:00:00"
      this.endTime = this.planTime + " 23:59:59"
      this.model.startTime = this.planTime + " 00:00:00"
      this.model.endTime = this.planTime + " 23:59:59"
      this.edit({}, 1)
    },
    edit(record, isEdit) {
      if (isEdit != 1) {
        this.model = Object.assign({}, record)
        this.startTime = this.model.startTime
        this.endTime = this.model.endTime
        this.time = moment(this.model.planTime)
      }

      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.disable = true
      this.$refs.form.resetFields()
      this.model.planTime = ''
      this.model.workshop = null
      this.model.planTime = ''
      this.model.productType = ''
      this.model.workingProcedure = ''
      this.model.productCategory = ''
      this.model.length = ''
      this.model.ration = ''
      this.model.rationRange = ''
      this.model.linearVelocity = ''
      this.model.linearVelocityRange = ''
      this.model.cottonBarrelSize = ''
      this.model.weight = ''
      this.model.fixedMachinesNum = ''
      this.model.unitPriceCoefficient = ''
      this.model.productTypeCoefficient = ''
      this.model.productionUnitPrice = ''
      this.model.cleanUnitPrice = ''
      this.model.barrelSize = ''
      this.model.machineCoefficient = ''
      this.model.deviceNo = ''
      this.model.deviceModel = ''
      this.model.filterLayerNum = ''
    },

    handleOk() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model)
          console.log('formData', formData)
          //时间格式化
          httpAction(httpurl, formData, method)
            .then((res) => {
              if (res.success) {
                this.$message.success(res.message != null ? res.message : '执行成功')
                this.$emit('ok')
              } else {
                this.$message.warning(res.message != null ? res.message : '执行失败')
              }
            })
            .finally(() => {
              this.confirmLoading = false
              this.close()
            })
        } else {
          return false
        }
      })
    },

    handleCancel() {
      this.close()
    },
  },
}
</script>

<style scoped>

</style>